package com.gxar.navigation.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.navigation.server.base.model.ResultCodeEnum;
import com.gxar.navigation.server.base.model.line.vo.request.LineCreateRequest;
import com.gxar.navigation.server.base.model.line.vo.request.LineUpdateRequest;
import com.gxar.navigation.server.base.model.line.vo.response.LineCreateResponse;
import com.gxar.navigation.server.base.model.line.vo.response.LineInfoResponse;
import com.gxar.navigation.server.base.model.line.vo.response.LinePageResponse;
import com.gxar.navigation.server.base.model.line.vo.response.LineUpdateResponse;
import com.gxar.navigation.server.db.service.LineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/9/12 14:06
 */
@Api(tags = "地图路段视图层")
@RestController
@RequestMapping("/api/map/line")
@RequiredArgsConstructor
@Slf4j
public class LineController {

    private final LineService lineService;


    /**
     * 创建路段
     *
     * @param lineCreateRequest 路段信息
     * @return 创建结果
     */
    @ApiOperation("创建路段")
    @PostMapping("/create/{active_id}")
    @ResponseBody
    public Result<LineCreateResponse> create(@PathVariable(name = "active_id") Long activeId, @RequestBody @Valid LineCreateRequest lineCreateRequest) {
        if (Objects.nonNull(lineCreateRequest.getDistance())) {
            if (lineCreateRequest.getDistance().compareTo(0d) < 0) {
                throw new APIRuntimeException(ResultCodeEnum.LINE_DISTANCE_MUST_POSITIVE_NUMBER);
            }
        }
        LineCreateResponse materialCreateResponse = lineService.create(activeId, lineCreateRequest);
        return Result.success(materialCreateResponse);
    }

    /**
     * 路段列表查询
     *
     * @param pageNum  当前页
     * @param pageSize 页记录数
     * @return 路段列表信息
     */
    @ApiOperation("路段列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "int", defaultValue = "10"),
    })
    @GetMapping("/list/{active_id}")
    public Result<PageResult<LinePageResponse>> pageList(@PathVariable(name = "active_id") Long activeId,
                                                         @RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                         @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {

        return lineService.pageList(activeId, pageNum, pageSize);
    }

    /**
     * 获取路段详情
     *
     * @param id 路段id
     * @return 路段详情
     */
    @ApiOperation("查看路段详情")
    @ApiImplicitParam(name = "id", value = "路段ID", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{active_id}/{id}")
    public Result<LineInfoResponse> getDetail(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id) {
        return Result.success(lineService.getDetail(activeId, id));
    }

    /**
     * 更新路段
     *
     * @param id                路段主键
     * @param lineUpdateRequest 路段信息
     * @return 更新结果
     */
    @ApiOperation("更新路段")
    @ApiImplicitParam(name = "id", value = "路段ID", dataType = "long", required = true, paramType = "path")
    @PutMapping("/{active_id}/{id}")
    public Result<LineUpdateResponse> update(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id, @RequestBody @Valid LineUpdateRequest lineUpdateRequest) {
        if (Objects.nonNull(lineUpdateRequest.getDistance())) {
            if (lineUpdateRequest.getDistance().compareTo(0d) < 0) {
                throw new APIRuntimeException(ResultCodeEnum.LINE_DISTANCE_MUST_POSITIVE_NUMBER);
            }
        }
        return Result.success(lineService.update(activeId, id, lineUpdateRequest));
    }

    /**
     * 删除路段
     *
     * @param id 路段id
     * @return 删除的路段id
     */
    @ApiOperation("删除路段")
    @ApiImplicitParam(name = "id", value = "路段ID", dataType = "long", required = true, paramType = "path")
    @DeleteMapping("/{active_id}/{id}")
    public Result<Boolean> delete(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id) {
        return Result.success(lineService.delete(activeId, id));
    }

}
